package com.cn.evaluate.handler;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
/**
 * 记评处理器
 * 
 * @author Admin 
 * 
 */
public class EvaluateHandler {
	private static Logger logger = Logger.getLogger(EvaluateHandler.class);

	/**
	 * 50米跑计算
	 * 
	 * @throws DocumentException
	 */

	public static void main(String[] args) throws DocumentException {
		/**
		 * 初始化记评参数
		 */
		double run_50_currentValue = 11.1;
		String run_50_xpath = "/subject/rulers/ruler[@grade='3' and @sex='0']/result";
		/**
		 * 初始化记评处理器
		 */
		EvaluateHandler handler = new EvaluateHandler();
		handler.setXmlFilePath("src/com/cn/evaluate/resource/run_50.xml");
		/**
		 * 计算记评
		 */
		Element element = handler.getRootElement();
		Element result = handler.getElement(element, run_50_xpath,
				run_50_currentValue);
		/**
		 * 处理记评结果
		 */
		logger.debug(result.asXML());
		logger.debug("rank=" + result.attributeValue("rank"));
		logger.debug("value=" + result.attributeValue("value"));
	}

	/**
	 * 身高体重计算
	 * 
	 * @param args
	 * @throws DocumentException
	 */
	// public static void main(String[] args) throws DocumentException {
	// /**
	// * 初始化记评参数
	// */
	// double height = 107.1;
	// double weight = 12;
	// String condition_xpath =
	// "/subject/rulers/ruler[@grade='3' and @sex='0']/condition";
	// String result_xpath = "/subject/rulers/ruler/condition/result";
	//
	// /**
	// * 初始化记评处理器
	// */
	// SubjectHandler handler = new SubjectHandler();
	// handler.setXmlFilePath("src/com/cn/evaluate/resource/height_weight.xml");
	//
	// /**
	// * 获取计算结果
	// */
	// Element element = handler.getRootElement();
	// Element condition = handler
	// .getElement(element, condition_xpath, height);
	// Element result = handler.getElement(condition, result_xpath, weight);
	//
	// /**
	// * 处理返回值
	// */
	// logger.debug(result.asXML());
	// logger.debug("rank=" + result.attributeValue("rank"));
	// logger.debug("value=" + result.attributeValue("value"));
	// }

	/**
	 * 依赖注入获取xml文件全路径
	 */
	public String xmlFilePath;

	/**
	 * 构造函数
	 */
	public EvaluateHandler() {

	}

	/**
	 * 获取学生测评成绩
	 * 
	 * @param element
	 * @param xpath
	 * @param currentValue
	 * @return xml元素
	 */
	public Element getElement(Element element, String xpath, double currentValue) {
		Element result = null;

		List<?> resultList = element.selectNodes(xpath);
		for (int i = 0; i < resultList.size(); ++i) {
			result = (Element) resultList.get(i);
			boolean isOk = true;
			if (this.includeAttribute(result, "gt")) {
				double targetValue = Double.parseDouble(result
						.attributeValue("gt"));
				isOk = isOk && (currentValue > targetValue);
			}
			if (this.includeAttribute(result, "ge")) {
				double targetValue = Double.parseDouble(result
						.attributeValue("ge"));
				isOk = isOk && (currentValue >= targetValue);
			}
			if (this.includeAttribute(result, "lt")) {
				double targetValue = Double.parseDouble(result
						.attributeValue("lt"));
				isOk = isOk && (currentValue < targetValue);
			}
			if (this.includeAttribute(result, "le")) {
				double targetValue = Double.parseDouble(result
						.attributeValue("le"));
				isOk = isOk && (currentValue <= targetValue);
			}

			if (isOk == true) {
				return result;
			}
		}
		return null;
	}

	/**
	 * 获取Root元素
	 * 
	 * @param xmlFilePath
	 * @return xml元素
	 * @throws DocumentException
	 */
	public Element getRootElement() throws DocumentException {
		File xmlFile = new File(xmlFilePath);
		SAXReader reader = new SAXReader();
		Document document = reader.read(xmlFile);
		Element root = document.getRootElement();
		return root;
	}

	/**
	 * 获取项目表述
	 * 
	 * @return 项目表述
	 * @throws DocumentException
	 */
	public String getSubjectDescript(String xmlFilePath)
			throws DocumentException {
		Element root = this.getRootElement();
		return root.attributeValue("descript");
	}

	public List<?> getSubjectField(String xmlFilePath) throws DocumentException {
		Element root = this.getRootElement();
		List<?> list = root.elements("field");
		for (int i = 0; i < list.size(); ++i) {
			Element element = (Element) list.get(i);
			logger.debug(element.attributeValue("name"));
			logger.debug(element.attributeValue("format"));
			logger.debug(element.attributeValue("unit"));
		}
		return list;
	}

	/**
	 * 获取项目名称
	 * 
	 * @return 项目名称
	 * @throws DocumentException
	 */
	public String getSubjectName(String xmlFilePath) throws DocumentException {
		Element root = this.getRootElement();
		return root.attributeValue("name");
	}

	public String getXmlFilePath() {
		return xmlFilePath;
	}

	/**
	 * 是否包含属性
	 * 
	 * @param element
	 * @param attributeName
	 * @return 是或否
	 */
	private boolean includeAttribute(Element element, String attributeName) {
		List<?> list = element.selectNodes("@" + attributeName);
		if (list.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public void setXmlFilePath(String xmlFilePath) {
		this.xmlFilePath = xmlFilePath;
	}
}
